package org.jeecg.modules.demo.ddyht.controller;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ddyht.entity.*;
import org.jeecg.modules.demo.ddyht.service.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.Utils.RandomUtil;
import org.jeecg.modules.demo.jxcx.po.*;
import org.jeecg.modules.demo.jxcx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 批次信息
 * @Author: jeecg-boot
 * @Date:   2025-02-23
 * @Version: V1.0
 */
@Api(tags="批次信息")
@RestController
@RequestMapping("/ddyht/jxcPici")
@Slf4j
public class DdygPiciController extends JeecgController<DdygPici, IDdygPiciService> {
	@Autowired
	private IDdygPiciService ddygPiciService;
	@Autowired
	private IDdygInventoryService ddygInventoryService;
	@Autowired
	private IDdygSaleService ddygSaleService;
	@Autowired
	private IDdygStockService ddygStockService;
	@Autowired
	private JxcPiciService jxcPiciService;
	@Autowired
	private JxcSaleService jxcSaleService;
	@Autowired
	private JxcStockService jxcStockService;
	@Autowired
	private JxcSupplierService jxcSupplierService;
	@Autowired
	private JxcCustomService jxcCustomService;
	@Autowired
	private JxcCzrzService jxcCzrzService;
	@Autowired
	private IJxcHbzjService jxcHbzjService;
     @Autowired
     private YueController yueController;

	/**
	 * 分页列表查询
	 *
	 * @param ddygPici
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "批次信息-分页列表查询")
	@ApiOperation(value="批次信息-分页列表查询", notes="批次信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DdygPici>> queryPageList(DdygPici ddygPici,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		QueryWrapper<DdygPici> queryWrapper = QueryGenerator.initQueryWrapper(ddygPici, req.getParameterMap());
		Page<DdygPici> page = new Page<DdygPici>(pageNo, pageSize);
		IPage<DdygPici> pageList = ddygPiciService.page(page, queryWrapper);
		List<DdygPici> company = ddygPiciService.getBycompany(ddygPici.getUsername());
		int size = company.size();
		int page2 = (size + 9) / 10;
		pageList.setTotal(size);
		pageList.setPages(page2);
		pageList.setRecords(company);
//		setamount(ddygPici.getUsername());
		return Result.OK(pageList);
	}
	@PostMapping(value = "/getBatchDetails")
	public Result<List<DdygPici>> getBatchDetails(@RequestBody JSONObject jsonObject) {
		String companyId = jsonObject.getString("companyId");
		String id = jsonObject.getString("id");
		String orstate = jsonObject.getString("orstate");
		List<DdygPici> byall = jxcPiciService.getByall(companyId, id, orstate);
		return Result.ok(byall);
	}
	@PostMapping(value = "/deleteDetails")
	public Result deleteDetails(@RequestBody JSONObject jsonObject) {
		LocalDateTime now = LocalDateTime.now();
		// 定义日期时间格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
		String formattedDateTime = now.format(formatter);

		String companyId = jsonObject.getString("companyId");
		String id = jsonObject.getString("id");
		log.info("id："+id);
		String orstate = jsonObject.getString("orstate");
		List<DdygPici> byall = ddygPiciService.getByallId(companyId, id);
		ddygPiciService.deleteByall(companyId, id, orstate);
		if (orstate.equals("1")){
			// 进货
			DdygStock StockbyId = ddygStockService.getById(id);
			BigDecimal StockAmount = new BigDecimal(StockbyId.getAmount());
			String traner =	StockbyId.getSeller();
			JxcCzrz jxcCzrz1 = new JxcCzrz();
//			log.info("000000");
			jxcCzrz1.setId(setczrzid(id));
			jxcCzrz1.setCompanyId(companyId);
			jxcCzrz1.setInfor("金额:"+StockbyId.getAmount());
			jxcCzrz1.setType("进货单删除");
			jxcCzrz1.setOther("pc端");
			jxcCzrzService.add(jxcCzrz1);
			if (ddygStockService.getById(id).getIsPay().equals("是")){
				JxcHbzj jxcHbzj = jxcHbzjService.selectbyPaymentMethod(StockbyId.getPaymentMethod(), companyId);
				jxcHbzj.setAmount(String.valueOf(new BigDecimal(jxcHbzj.getAmount()).add(new BigDecimal(StockbyId.getAmount()))));
				jxcHbzj.setCreateTime(formattedDateTime);
				jxcHbzjService.updateById(jxcHbzj);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
				jxcCzrz2.setOtherid("货币资金");
				jxcCzrz2.setOrstate("进货单批次退货-" + StockbyId.getSeller());
				jxcCzrz2.setType(StockbyId.getPaymentMethod());
				jxcCzrz2.setInfor("+" + StockbyId.getAmount());
				jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setHbzj(jxcCzrz2,StockbyId.getPaymentMethod(),StockbyId.getAmount(),"+");
				jxcCzrzService.add(jxcCzrz2);
			}else {
				JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
				if (jxcSupplier != null){
					BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
					jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(StockAmount)));
					jxcSupplierService.updateById(jxcSupplier);

					JxcCzrz jxcCzrz2 = new JxcCzrz();
					jxcCzrz2.setId(setczrzid(id));
					jxcCzrz2.setCompanyId(companyId);
//					jxcCzrz2.setInfor("金额:"+oldSupplieramount+"->"+oldSupplieramount.subtract(StockAmount));
//					jxcCzrz2.setType("供应商修改");
                    jxcCzrz2.setOtherid("应收应付-进货单删除");
                    jxcCzrz2.setOrstate("应付款-"+traner);
                    jxcCzrz2.setType("应付款-"+traner);
                    jxcCzrz2.setInfor("进货单删除：-" + StockAmount);
                    log.info("creatTime:{}",  formattedDateTime);
                    jxcCzrz2.setCreatTime(formattedDateTime);
                    yueController.setSupplier(jxcCzrz2,traner,StockAmount.toString(),"进货单删除：-");
					jxcCzrz2.setOther("pc端");
					jxcCzrzService.add(jxcCzrz2);
				}
			}
			ddygStockService.removeById(id);
			for (DdygPici ddygPici : byall) {
				String model = ddygPici.getModel();
				String number = ddygPici.getNumber();
				String unitPrice = ddygPici.getUnitPrice();
				if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
					ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(number))));
					BigDecimal multiply = new BigDecimal(unitPrice).multiply(new BigDecimal(number));
//					ddygInventory.setAmount(String.valueOf(new BigDecimal(ddygInventory.getAmount()).subtract(multiply)));
					ddygInventoryService.updateById(ddygInventory);
				}
			}
		}else {
			// 销售
			DdygSale SalebyId = ddygSaleService.getById(id);
			BigDecimal SaleAmount = new BigDecimal(SalebyId.getAmount());
			String traner =	SalebyId.getBuyer();
			JxcCzrz jxcCzrz1 = new JxcCzrz();
//			log.info("111111");
			jxcCzrz1.setId(setczrzid(id));
			jxcCzrz1.setCompanyId(companyId);
			jxcCzrz1.setInfor("金额:"+SalebyId.getAmount());
			jxcCzrz1.setType("销货单删除");
			jxcCzrz1.setOther("pc端");
			jxcCzrzService.add(jxcCzrz1);
			if (ddygSaleService.getById(id).getIsPay().equals("是")){
				JxcHbzj jxcHbzj = jxcHbzjService.selectbyPaymentMethod(SalebyId.getPaymentMethod(), companyId);
				jxcHbzj.setAmount(String.valueOf(new BigDecimal(jxcHbzj.getAmount()).subtract(new BigDecimal(SalebyId.getAmount()))));
				jxcHbzj.setCreateTime(formattedDateTime);
				jxcHbzjService.updateById(jxcHbzj);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
//				log.info("222222");
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
				jxcCzrz2.setOtherid("货币资金");
				jxcCzrz2.setOrstate("销货单批次退货-" + SalebyId.getBuyer());
				jxcCzrz2.setType(SalebyId.getPaymentMethod());
				jxcCzrz2.setInfor("-" + SalebyId.getAmount());
				jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setHbzj(jxcCzrz2,SalebyId.getPaymentMethod(),SalebyId.getAmount(),"-");
				jxcCzrzService.add(jxcCzrz2);
			}else {
				JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
				if (jxcCustom != null){
					BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
					jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(SaleAmount)));
					jxcCustomService.updateById(jxcCustom);

					JxcCzrz jxcCzrz2 = new JxcCzrz();
//					log.info("333333");
					jxcCzrz2.setId(setczrzid(id));
					jxcCzrz2.setCompanyId(companyId);
//					jxcCzrz2.setInfor("金额:"+oldCustomamount+"->"+oldCustomamount.subtract(SaleAmount));
//					jxcCzrz2.setType("客户修改");
					jxcCzrz2.setOther("pc端");
                    jxcCzrz2.setOtherid("应收应付-销货单删除");
                    jxcCzrz2.setOrstate("应收款-"+traner);
                    jxcCzrz2.setType("应收款-"+traner);
                    jxcCzrz2.setInfor("销货单删除：-" + jxcCustom.getAmount());
                    log.info("creatTime:{}",  formattedDateTime);
                    jxcCzrz2.setCreatTime(formattedDateTime);
                    yueController.setCustom(jxcCzrz2,traner,jxcCustom.getAmount(),"销货单删除：-");
					jxcCzrzService.add(jxcCzrz2);
				}
			}
			ddygSaleService.removeById(id);
			for (DdygPici ddygPici : byall) {
				String model = ddygPici.getModel();
				String number = ddygPici.getNumber();
				String unitPrice = ddygPici.getUnitPrice();
				if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
					ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).add(new BigDecimal(number))));
					BigDecimal multiply = new BigDecimal(unitPrice).multiply(new BigDecimal(number));
//					ddygInventory.setAmount(String.valueOf(new BigDecimal(ddygInventory.getAmount()).subtract(multiply)));
					ddygInventoryService.updateById(ddygInventory);
				}
			}
		}

		return Result.ok("删除成功");
	}
	 @PostMapping("/queryalltwo")
	 @ResponseBody
	 public Result queryalltwo(@RequestBody JSONObject params) {
		 String companyId = params.getString("companyId");
		 String model = params.getString("model").trim();
		 String orstate = params.getString("orstate");
		 return Result.ok(ddygPiciService.getByalltwo(companyId, model, orstate));
	 }

	/**
	 *   添加
	 *
	 * @param ddygPici
	 * @return
	 */
	@AutoLog(value = "批次信息-添加")
	@ApiOperation(value="批次信息-添加", notes="批次信息-添加")
//	@RequiresPermissions("ddyht:jxc_pici:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody List<DdygPici> ddygPici) {
		return ddygPiciService.add(ddygPici);
	}

	/**
	 *  编辑
	 *
	 * @param jxcPici
	 * @return
	 */
	@AutoLog(value = "批次信息-编辑")
	@ApiOperation(value="批次信息-编辑", notes="批次信息-编辑")
//	@RequiresPermissions("ddyht:jxc_pici:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DdygPici jxcPici) {
		LocalDateTime now = LocalDateTime.now();
		// 定义日期时间格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
		String formattedDateTime = now.format(formatter);
		log.info("数据修改开始{}",jxcPici.toString());
		String onlyid = jxcPici.getOnlyid();
		String id = String.valueOf(jxcPici.getId());
		String orstate = jxcPici.getOrstate();
		String model = jxcPici.getModel();
		String traner = jxcPici.getTraner().trim();
		String companyId = jxcPici.getCompanyId();
		//获取修改前的数据
		JxcPici oldjxcPici = jxcPiciService.getByOnlyid(onlyid);
		BigDecimal newnumber = new BigDecimal(jxcPici.getNumber());
		BigDecimal newunitPrice = new BigDecimal(jxcPici.getUnitPrice());
		BigDecimal oldnumber = new BigDecimal(oldjxcPici.getNumber());
		String oldModel = oldjxcPici.getModel();
		if (orstate.equals("1")){
			//进货
			JxcStock jxcStock = jxcStockService.getById(id);
			JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
			BigDecimal oldallamount = new BigDecimal(jxcStock.getAmount());
			BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
			BigDecimal oldamount = new BigDecimal(oldjxcPici.getAmount());
			BigDecimal newamount = newnumber.multiply(newunitPrice);
			log.info("oldamount:"+oldamount);
			log.info("newamount:"+newamount);

			if (!model.equals(oldModel)){
				List<String> modellist = new ArrayList<>();
				List<DdygPici> ddygPicis = ddygPiciService.getByallId(companyId,id);
				modellist.add(model);
				for (int i = 0; i < ddygPicis.size(); i++) {
					DdygPici ddygPici = ddygPicis.get(i);
					if (!ddygPici.getOnlyid().equals(onlyid)){
						modellist.add(ddygPici.getModel());
					}
				}
				jxcStock.setModel(modellist.toString());
			}

			DdygPici oldddygPici = ddygPiciService.getByOnlyId(onlyid, companyId);
			if (StringUtils.isNotBlank(oldddygPici.getRealnumber())) {
				if (oldnumber.compareTo(newnumber) > 0) {
					oldddygPici.setRealnumber(String.valueOf(new BigDecimal(oldddygPici.getRealnumber()).subtract(oldnumber.subtract(newnumber))));
					ddygPiciService.updateById(oldddygPici);
				}else {
					oldddygPici.setRealnumber(String.valueOf(new BigDecimal(oldddygPici.getRealnumber()).add(newnumber.subtract(oldnumber))));
					ddygPiciService.updateById(oldddygPici);
				}
			}
			if (newamount.compareTo(oldamount) > 0){
//				如果金额比原来多
				BigDecimal lisamount = newamount.subtract(oldamount);
				jxcStock.setAmount(String.valueOf(oldallamount.add(lisamount)));
				jxcStockService.updateById(jxcStock);
				updateCzrzById(jxcStock.getId(),String.valueOf(oldallamount.add(lisamount)),companyId);

				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null){
						DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
						JxcCzrz jxcCzrz1 = new JxcCzrz();
						jxcCzrz1.setId(setczrzid(id));
						jxcCzrz1.setCompanyId(companyId);
						BigDecimal number = new BigDecimal(ddygInventory.getNumber());

					if (!oldModel.equals(model)){
						jxcCzrz1.setInfor("型号:"+oldModel+"->"+model);
						ddygInventory.setNumber(String.valueOf(number.subtract(oldnumber)));
						if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).add(newnumber)));
							ddygInventoryService.updateById(bymodelcompany);
						}else {
							DdygInventory bymodelcompany = new DdygInventory();
							bymodelcompany.setNumber(String.valueOf(newnumber));
							bymodelcompany.setModel(model);
							bymodelcompany.setCompanyId(companyId);
							bymodelcompany.setCreateTime(formattedDateTime);
							ddygInventoryService.save(bymodelcompany);
						}
					}else {
						if (oldnumber.compareTo(newnumber) > 0){
							ddygInventory.setNumber(String.valueOf(number.subtract(oldnumber.subtract(newnumber))));
							jxcCzrz1.setInfor("数量:-"+String.valueOf(oldnumber.subtract(newnumber)));
						}else {
							ddygInventory.setNumber(String.valueOf(number.add(newnumber.subtract(oldnumber))));
							jxcCzrz1.setInfor("数量:+"+String.valueOf(newnumber.subtract(oldnumber)));
						}
					}
						ddygInventoryService.updateById(ddygInventory);
						jxcCzrz1.setType("库存修改");
						jxcCzrz1.setOther("pc端");
						R add = jxcCzrzService.add(jxcCzrz1);
				}

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:+"+String.valueOf(lisamount));
				jxcCzrz1.setType("进货单修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);

				jxcSupplier.setAmount(String.valueOf(oldSupplieramount.add(lisamount)));
				jxcSupplierService.updateById(jxcSupplier);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
				jxcCzrz2.setInfor("金额:+"+String.valueOf(lisamount));
//				jxcCzrz2.setType("供应商修改");
//				jxcCzrz2.setOther("pc端");
                jxcCzrz2.setOtherid("应收应付-进货单修改");
                jxcCzrz2.setOrstate("应付款-"+traner);
                jxcCzrz2.setType("应付款-"+traner);
                jxcCzrz2.setInfor("进货单修改：+" + lisamount);
                log.info("creatTime:{}",  formattedDateTime);
                jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setSupplier(jxcCzrz2,traner,lisamount.toString(),"进货单修改：+");
				R add2 = jxcCzrzService.add(jxcCzrz2);
			}else if (newamount.compareTo(oldamount) < 0){
				//如果金额比原来少
				BigDecimal lisamount = oldamount.subtract(newamount);
				jxcStock.setAmount(String.valueOf(oldallamount.subtract(lisamount)));
//                jxcStock.setModel(jxcStock.getModel()+jxcPici.getModel());
				jxcStockService.updateById(jxcStock);
				updateCzrzById(jxcStock.getId(),String.valueOf(oldallamount.subtract(lisamount)),companyId);

				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					BigDecimal number = new BigDecimal(ddygInventory.getNumber());
					if (!oldModel.equals(model)){
						jxcCzrz1.setInfor("型号:"+oldModel+"->"+model);
						ddygInventory.setNumber(String.valueOf(number.subtract(oldnumber)));
						if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).add(newnumber)));
							ddygInventoryService.updateById(bymodelcompany);
						}else {
							DdygInventory bymodelcompany = new DdygInventory();
							bymodelcompany.setNumber(String.valueOf(newnumber));
							bymodelcompany.setModel(model);
							bymodelcompany.setCompanyId(companyId);
							bymodelcompany.setCreateTime(formattedDateTime);
							ddygInventoryService.save(bymodelcompany);
						}
					}else {
						if (oldnumber.compareTo(newnumber) > 0) {
							ddygInventory.setNumber(String.valueOf(number.subtract(oldnumber.subtract(newnumber))));
							jxcCzrz1.setInfor("数量:-" + String.valueOf(oldnumber.subtract(newnumber)));
						} else {
							ddygInventory.setNumber(String.valueOf(number.add(newnumber.subtract(oldnumber))));
							jxcCzrz1.setInfor("数量:+" + String.valueOf(newnumber.subtract(oldnumber)));
						}
					}
					ddygInventoryService.updateById(ddygInventory);
					jxcCzrz1.setType("库存修改");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}
				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:-"+String.valueOf(lisamount));
				jxcCzrz1.setType("进货单修改");
				R add = jxcCzrzService.add(jxcCzrz1);

				jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(lisamount)));
				jxcSupplierService.updateById(jxcSupplier);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
//				jxcCzrz2.setInfor("金额:-"+String.valueOf(lisamount));
//				jxcCzrz2.setType("供应商修改");
                jxcCzrz2.setOtherid("应收应付-进货单修改");
                jxcCzrz2.setOrstate("应付款-"+traner);
                jxcCzrz2.setType("应付款-"+traner);
                jxcCzrz2.setInfor("进货单修改：-" + lisamount);
                log.info("creatTime:{}",  formattedDateTime);
                jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setSupplier(jxcCzrz2,traner,lisamount.toString(),"进货单修改：-");
				R add2 = jxcCzrzService.add(jxcCzrz2);
			}else {
				jxcStockService.updateById(jxcStock);
				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null) {
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					BigDecimal number = new BigDecimal(ddygInventory.getNumber());
					if (!oldModel.equals(model)) {
						jxcCzrz1.setInfor("型号:" + oldModel + "->" + model);
						ddygInventory.setNumber(String.valueOf(number.subtract(oldnumber)));
						if (ddygInventoryService.getBymodelcompany(model, companyId) != null) {
							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).add(newnumber)));
							ddygInventoryService.updateById(bymodelcompany);
						}else {
							DdygInventory bymodelcompany = new DdygInventory();
							bymodelcompany.setNumber(String.valueOf(newnumber));
							bymodelcompany.setModel(model);
							bymodelcompany.setCompanyId(companyId);
							bymodelcompany.setCreateTime(formattedDateTime);
							ddygInventoryService.save(bymodelcompany);
						}
					}
					ddygInventoryService.updateById(ddygInventory);
				}
			}
			JxcCzrz jxcCzrz = new JxcCzrz();
			jxcCzrz.setId(setczrzid(id));
			jxcCzrz.setCompanyId(companyId);
			String infor = "金额:"+String.valueOf(oldamount)+"->"+String.valueOf(newamount);
			jxcCzrz.setType("进货单批次修改");

//			if (!oldjxcPici.getModel().equals(jxcPici.getModel())) {
//				jxcStock.setModel(jxcStock.getModel() + jxcPici.getModel());
//				jxcStockService.updateById(jxcStock);
//				infor += "型号->"+jxcPici.getModel();
//			}
			jxcCzrz.setInfor(infor);
			jxcCzrz.setOther("pc端");
			R add3 = jxcCzrzService.add(jxcCzrz);
			jxcPiciService.updateByOnlyid(jxcPici.getModel(),String.valueOf(newnumber),String.valueOf(newunitPrice), String.valueOf(newamount),onlyid);
		return Result.OK("编辑成功!");
		}else {
			//销售
			JxcSale jxcSale = jxcSaleService.getById(id);
			JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
			BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
			BigDecimal oldallamount = new BigDecimal(jxcSale.getAmount());
			BigDecimal oldamount = new BigDecimal(oldjxcPici.getAmount());
			BigDecimal newamount = newnumber.multiply(newunitPrice);

			if (!model.equals(oldModel)){
				List<String> modellist = new ArrayList<>();
				List<DdygPici> ddygPicis = ddygPiciService.getByallId(companyId,id);
				modellist.add(model);
				for (int i = 0; i < ddygPicis.size(); i++) {
					DdygPici ddygPici = ddygPicis.get(i);
					if (!ddygPici.getOnlyid().equals(onlyid)){
						modellist.add(ddygPici.getModel());
					}
				}
				jxcSale.setModel(modellist.toString());
			}

			if (newamount.compareTo(oldamount) > 0) {
				//如果金额比原来多
				BigDecimal lisamount = newamount.subtract(oldamount);
//				BigDecimal lisamount = new BigDecimal(newamount.compareTo(oldamount));
				jxcSale.setAmount(String.valueOf(oldallamount.add(lisamount)));
//                jxcSale.setModel(jxcSale.getModel()+jxcPici.getModel());
				jxcSaleService.updateById(jxcSale);
				updateCzrzById(jxcSale.getId(),String.valueOf(oldallamount.add(lisamount)),companyId);

//				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null){
//					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
//					JxcCzrz jxcCzrz1 = new JxcCzrz();
//					jxcCzrz1.setId(setczrzid(id));
//					jxcCzrz1.setCompanyId(companyId);
//					BigDecimal number = new BigDecimal(ddygInventory.getNumber());
//					if (!oldModel.equals(model)){
//						jxcCzrz1.setInfor("型号:"+oldModel+"->"+model);
//						ddygInventory.setNumber(String.valueOf(number.add(oldnumber)));
//						if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
//							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
//							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).subtract(newnumber)));
//							ddygInventoryService.updateById(bymodelcompany);
//						}
//					}else {
//						if (oldnumber.compareTo(newnumber) > 0) {
//							ddygInventory.setNumber(String.valueOf(number.add(oldnumber.subtract(newnumber))));
//							jxcCzrz1.setInfor("数量:+" + String.valueOf(oldnumber.subtract(newnumber)));
//						} else {
//							ddygInventory.setNumber(String.valueOf(number.subtract(newnumber.subtract(oldnumber))));
//							jxcCzrz1.setInfor("数量:-" + String.valueOf(newnumber.subtract(oldnumber)));
//						}
//					}
//					ddygInventoryService.updateById(ddygInventory);
//					jxcCzrz1.setType("库存修改");
//					jxcCzrz1.setOther("pc端");
//					R add = jxcCzrzService.add(jxcCzrz1);
//				}

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:+" + String.valueOf(lisamount));
				jxcCzrz1.setType("销货单修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);

				jxcCustom.setAmount(String.valueOf(oldCustomamount.add(lisamount)));
				jxcCustomService.updateById(jxcCustom);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
                jxcCzrz2.setOtherid("应收应付-销货单修改");
                jxcCzrz2.setOrstate("应收款-"+traner);
                jxcCzrz2.setType("应收款-"+traner);
                jxcCzrz2.setInfor("销货单修改：+" + lisamount);
                log.info("creatTime:{}",  formattedDateTime);
                jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setCustom(jxcCzrz2,traner,lisamount.toString(),"销货单修改：+");
				R add2 = jxcCzrzService.add(jxcCzrz2);
			} else if (newamount.compareTo(oldamount) < 0) {
				//如果金额比原来少
				BigDecimal lisamount = oldamount.subtract(newamount);
				jxcSale.setAmount(String.valueOf(oldallamount.subtract(lisamount)));
//                jxcSale.setModel(jxcSale.getModel()+jxcPici.getModel());
				jxcSaleService.updateById(jxcSale);
				updateCzrzById(jxcSale.getId(),String.valueOf(oldallamount.subtract(lisamount)),companyId);

//				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null){
//					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
//					JxcCzrz jxcCzrz1 = new JxcCzrz();
//					jxcCzrz1.setId(setczrzid(id));
//					jxcCzrz1.setCompanyId(companyId);
//					BigDecimal number = new BigDecimal(ddygInventory.getNumber());
//
//					if (!oldModel.equals(model)){
//						jxcCzrz1.setInfor("型号:"+oldModel+"->"+model);
//						ddygInventory.setNumber(String.valueOf(number.add(oldnumber)));
//						if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
//							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
//							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).subtract(newnumber)));
//							ddygInventoryService.updateById(bymodelcompany);
//						}
//					}else {
//						if (oldnumber.compareTo(newnumber) > 0) {
//							ddygInventory.setNumber(String.valueOf(number.add(oldnumber.subtract(newnumber))));
//							jxcCzrz1.setInfor("数量:+" + String.valueOf(oldnumber.subtract(newnumber)));
//						} else {
//							ddygInventory.setNumber(String.valueOf(number.subtract(newnumber.subtract(oldnumber))));
//							jxcCzrz1.setInfor("数量:-" + String.valueOf(newnumber.subtract(oldnumber)));
//						}
//					}
//					ddygInventoryService.updateById(ddygInventory);
//					jxcCzrz1.setType("库存修改");
//					jxcCzrz1.setOther("pc端");
//					R add = jxcCzrzService.add(jxcCzrz1);
//				}

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:-" + String.valueOf(lisamount));
				jxcCzrz1.setType("销货单修改");
				R add = jxcCzrzService.add(jxcCzrz1);

				jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(lisamount)));
				jxcCustomService.updateById(jxcCustom);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
//				jxcCzrz2.setInfor("金额:-" + String.valueOf(lisamount));
//				jxcCzrz2.setType("客户修改");
                jxcCzrz2.setOtherid("应收应付-销货单修改");
                jxcCzrz2.setOrstate("应收款-"+traner);
                jxcCzrz2.setType("应收款-"+traner);
                jxcCzrz2.setInfor("销货单修改：-" + lisamount);
                log.info("creatTime:{}",  formattedDateTime);
                jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setCustom(jxcCzrz2,traner,lisamount.toString(),"销货单修改：-");
				R add2 = jxcCzrzService.add(jxcCzrz2);

			}else {
				jxcSaleService.updateById(jxcSale);
//				if (ddygInventoryService.getBymodelcompany(oldModel, companyId) != null) {
//					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(oldModel, companyId);
//					JxcCzrz jxcCzrz1 = new JxcCzrz();
//					jxcCzrz1.setId(setczrzid(id));
//					jxcCzrz1.setCompanyId(companyId);
//					BigDecimal number = new BigDecimal(ddygInventory.getNumber());
//					if (!oldModel.equals(model)) {
//						jxcCzrz1.setInfor("型号:" + oldModel + "->" + model);
//						ddygInventory.setNumber(String.valueOf(number.add(oldnumber)));
//						if (ddygInventoryService.getBymodelcompany(model, companyId) != null) {
//							DdygInventory bymodelcompany = ddygInventoryService.getBymodelcompany(model, companyId);
//							bymodelcompany.setNumber(String.valueOf(new BigDecimal(bymodelcompany.getNumber()).subtract(number)));
//							ddygInventoryService.updateById(bymodelcompany);
//						}
//					}
//					ddygInventoryService.updateById(ddygInventory);
//				}
			}
			JxcCzrz jxcCzrz = new JxcCzrz();
			jxcCzrz.setId(setczrzid(id));
			jxcCzrz.setCompanyId(companyId);
			String infor = "金额:" + String.valueOf(oldamount) + "->" + String.valueOf(newamount);
			jxcCzrz.setType("销货单批次修改");
//			if (!oldjxcPici.getModel().equals(jxcPici.getModel())) {
//				jxcSale.setModel(jxcSale.getModel() + jxcPici.getModel());
//				jxcSaleService.updateById(jxcSale);
//				infor += "型号->" + jxcPici.getModel();
//			}
			jxcCzrz.setInfor(infor);
			jxcCzrz.setOther("pc端");
			R add3 = jxcCzrzService.add(jxcCzrz);
			jxcPiciService.updateByOnlyid(jxcPici.getModel(), String.valueOf(newnumber), String.valueOf(newunitPrice), String.valueOf(newamount), onlyid);
		return Result.OK("编辑成功!");
		}

//		ddygPiciService.updateById(ddygPici);
//		return Result.OK("编辑成功!");
	}

	 @RequestMapping(value = "/exitnum", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody JSONObject jsonObject) {
		String exitnum = jsonObject.getString("exitnum");
		String onlyid = jsonObject.getString("onlyid");
		String companyId = jsonObject.getString("companyId");
		String infor = jsonObject.getString("infor");
		DdygPici byId = ddygPiciService.getByOnlyId(onlyid, companyId);
		DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(byId.getModel(), companyId);
		LocalDateTime now = LocalDateTime.now();
		// 定义日期时间格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
		String formattedDateTime = now.format(formatter);
		BigDecimal exitAmount = new BigDecimal(byId.getUnitPrice()).multiply(new BigDecimal(exitnum));
		JxcStock jxcStock = jxcStockService.getById(byId.getId());
		jxcStock.setAmount(String.valueOf(new BigDecimal(jxcStock.getAmount()).subtract(exitAmount)));
//		jxcStock.setCreateTime(formattedDateTime);
		jxcStockService.updateById(jxcStock);

		JxcSupplier jxcSupplier = jxcSupplierService.getBy(byId.getTraner(), companyId);
		if (jxcSupplier != null){
			BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
			jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(new BigDecimal(byId.getUnitPrice()).multiply(new BigDecimal(exitnum)))));
			jxcSupplierService.updateById(jxcSupplier);

			JxcCzrz jxcCzrz1 = new JxcCzrz();
			jxcCzrz1.setId(setczrzid(byId.getId()));
			jxcCzrz1.setCompanyId(companyId);
			jxcCzrz1.setInfor("退货"+"(数量"+exitnum+")"+"金额:"+exitAmount);
			jxcCzrz1.setType("应付款-"+byId.getTraner());
			jxcCzrz1.setOrstate("应付款-"+byId.getTraner());
			jxcCzrz1.setOtherid("应收应付-退货");
			jxcCzrz1.setOther("pc端");
            jxcCzrz1.setCreatTime(formattedDateTime);
            yueController.setReturnSupplier(jxcCzrz1,byId.getTraner(),exitAmount.toString(),"应收应付-退货：-");
			R add = jxcCzrzService.add(jxcCzrz1);
		}

		if (infor.equals("NoReal")){
			byId.setNumber(String.valueOf(new BigDecimal(byId.getNumber()).subtract(new BigDecimal(exitnum))));
			byId.setAmount(String.valueOf(new BigDecimal(byId.getAmount()).subtract(new BigDecimal(byId.getUnitPrice()).multiply(new BigDecimal(exitnum)))));
			ddygPiciService.updateById(byId);
			ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(exitnum))));
			ddygInventory.setCreateTime(formattedDateTime);
			ddygInventoryService.updateById(ddygInventory);
			JxcCzrz jxcCzrz = new JxcCzrz();
			jxcCzrz.setId(setczrzid(byId.getId()));
			jxcCzrz.setCompanyId(companyId);
			jxcCzrz.setInfor("型号:"+byId.getModel()+"退货数量:"+exitnum+"退货金额:"+String.valueOf(new BigDecimal(byId.getAmount()).multiply(new BigDecimal(exitnum))));
			jxcCzrz.setType("批次号:"+byId.getOnlyid()+"进货单批次退货-NoReal");
            jxcCzrz.setOtherid("进货单批次退货");
            jxcCzrz.setCreatTime(formattedDateTime);
            jxcCzrz.setOrstate(byId.getId().substring(0,8));
			R add = jxcCzrzService.add(jxcCzrz);

            return Result.ok("退货成功NoReal");
		}else if (infor.equals("Real")){
			byId.setNumber(String.valueOf(new BigDecimal(byId.getNumber()).subtract(new BigDecimal(exitnum))));
			byId.setAmount(String.valueOf(new BigDecimal(byId.getAmount()).subtract(new BigDecimal(byId.getUnitPrice()).multiply(new BigDecimal(exitnum)))));
			byId.setRealnumber(String.valueOf(new BigDecimal(byId.getRealnumber()).subtract(new BigDecimal(exitnum))));
			ddygPiciService.updateById(byId);
//			DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(byId.getModel(), companyId);
			ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(exitnum))));
			ddygInventory.setCreateTime(formattedDateTime);
			ddygInventoryService.updateById(ddygInventory);
			JxcCzrz jxcCzrz = new JxcCzrz();
			jxcCzrz.setId(setczrzid(byId.getId()));
			jxcCzrz.setCompanyId(companyId);
			jxcCzrz.setInfor("型号:"+byId.getModel()+"退货数量:"+exitnum+"退货金额:"+String.valueOf(new BigDecimal(byId.getAmount()).multiply(new BigDecimal(exitnum))));
			jxcCzrz.setType("批次号:"+byId.getOnlyid()+"进货单批次退货-Real");
            jxcCzrz.setOtherid("进货单批次退货");
            jxcCzrz.setCreatTime(formattedDateTime);
            jxcCzrz.setOrstate(byId.getId().substring(0,8));
			R add = jxcCzrzService.add(jxcCzrz);
			return Result.ok("退货成功Real");
		}
		 return Result.ok(onlyid);
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "批次信息-通过id删除")
	@ApiOperation(value="批次信息-通过id删除", notes="批次信息-通过id删除")
//	@RequiresPermissions("ddyht:jxc_pici:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		LocalDateTime now = LocalDateTime.now();
		// 定义日期时间格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
		String formattedDateTime = now.format(formatter);
		DdygPici byId = ddygPiciService.getById(id);
		String orstate = byId.getOrstate();
		String companyId = byId.getCompanyId();
		String model = byId.getModel();
		String number = byId.getNumber();
		String unitPrice = byId.getUnitPrice();
		String piciid = byId.getId();
		String traner = byId.getTraner().trim();
		//去除空格

		log.info("批次删除数据"+byId.toString());
		String allamount = "";
		JxcPici jxcPici = jxcPiciService.getByOnlyid(id);
		BigDecimal pcAmount = new BigDecimal(jxcPici.getAmount());

		JxcCzrz jxcCzrz = new JxcCzrz();
		jxcCzrz.setId(setczrzid(id));
		jxcCzrz.setCompanyId(companyId);
		jxcCzrz.setInfor("批次删除：金额"+String.valueOf(pcAmount));

		if (orstate.equals("1")){
			jxcCzrz.setType("进货单批次删除");
			jxcCzrz.setOther("pc端");
			jxcCzrzService.add(jxcCzrz);
			//进货
			JxcStock jxcstock = jxcStockService.getallamount(piciid, companyId);
			JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);

			allamount = jxcstock.getAmount();
			BigDecimal bigDecimal = new BigDecimal(allamount);
			BigDecimal subtract = bigDecimal.subtract(pcAmount);
			String s = String.valueOf(subtract);
			if (subtract.compareTo(BigDecimal.ZERO) <= 0){
				jxcStockService.removeById(piciid);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
				jxcCzrz1.setType("进货单删除");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}else {
				jxcstock.setAmount(s);
				jxcStockService.updateById(jxcstock);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
				jxcCzrz1.setType("进货单修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}
			if (jxcSupplier != null){
				BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
				jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(pcAmount)));
				jxcSupplierService.updateById(jxcSupplier);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(oldSupplieramount)+"->"+String.valueOf(oldSupplieramount.subtract(pcAmount)));
				jxcCzrz1.setType("供应商修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}
			if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
					ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(number))));
					BigDecimal multiply = new BigDecimal(unitPrice).multiply(new BigDecimal(number));
//					ddygInventory.setAmount(String.valueOf(new BigDecimal(ddygInventory.getAmount()).subtract(multiply)));
					ddygInventoryService.updateById(ddygInventory);
			}
			if (jxcstock.getIsPay().equals("是")){
				JxcHbzj jxcHbzj = jxcHbzjService.selectbyPaymentMethod(byId.getPaymentMethod(), companyId);
				jxcHbzj.setAmount(String.valueOf(new BigDecimal(jxcHbzj.getAmount()).add(new BigDecimal(byId.getAmount()))));
				jxcHbzj.setCreateTime(formattedDateTime);
				jxcHbzjService.updateById(jxcHbzj);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
				jxcCzrz2.setOtherid("货币资金");
				jxcCzrz2.setOrstate("进货单批次退货");
				jxcCzrz2.setType(byId.getPaymentMethod());
				jxcCzrz2.setInfor("+" + byId.getAmount());
				jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setHbzj(jxcCzrz2,byId.getPaymentMethod(),byId.getAmount(),"+");
				jxcCzrzService.add(jxcCzrz2);
			}
		}else {
			jxcCzrz.setType("销货单批次删除");
			jxcCzrz.setOther("pc端");
			jxcCzrzService.add(jxcCzrz);

			JxcSale jxcSale = jxcSaleService.getallamount(piciid, companyId);
			JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);

			allamount = jxcSale.getAmount();
			BigDecimal bigDecimal = new BigDecimal(allamount);
			BigDecimal subtract = bigDecimal.subtract(pcAmount);
			String s = String.valueOf(subtract);
			if (subtract.compareTo(BigDecimal.ZERO) <= 0){
//			if (s.equals("0.00")){
				jxcSaleService.removeById(piciid);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
				jxcCzrz1.setType("销货单删除");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}else {
				jxcSale.setAmount(s);
				jxcSaleService.updateById(jxcSale);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
				jxcCzrz1.setType("销货单修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}
			if (jxcCustom != null){
				BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
				jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(pcAmount)));
				jxcCustomService.updateById(jxcCustom);

				JxcCzrz jxcCzrz1 = new JxcCzrz();
				jxcCzrz1.setId(setczrzid(id));
				jxcCzrz1.setCompanyId(companyId);
				jxcCzrz1.setInfor("金额:"+String.valueOf(oldCustomamount)+"->"+String.valueOf(oldCustomamount.subtract(pcAmount)));
				jxcCzrz1.setType("客户修改");
				jxcCzrz1.setOther("pc端");
				R add = jxcCzrzService.add(jxcCzrz1);
			}
			if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
				DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
				ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).add(new BigDecimal(number))));
				ddygInventoryService.updateById(ddygInventory);
			}
			if (jxcSale.getIsPay().equals("是")){
				JxcHbzj jxcHbzj = jxcHbzjService.selectbyPaymentMethod(byId.getPaymentMethod(), companyId);
				jxcHbzj.setAmount(String.valueOf(new BigDecimal(jxcHbzj.getAmount()).subtract(new BigDecimal(byId.getAmount()))));
				jxcHbzj.setCreateTime(formattedDateTime);
				jxcHbzjService.updateById(jxcHbzj);

				JxcCzrz jxcCzrz2 = new JxcCzrz();
				jxcCzrz2.setId(setczrzid(id));
				jxcCzrz2.setCompanyId(companyId);
				jxcCzrz2.setOtherid("货币资金");
				jxcCzrz2.setOrstate("销货单批次退货");
				jxcCzrz2.setType(byId.getPaymentMethod());
				jxcCzrz2.setInfor("-" + byId.getAmount());
				jxcCzrz2.setCreatTime(formattedDateTime);
                yueController.setHbzj(jxcCzrz2,byId.getPaymentMethod(),byId.getAmount(),"-");
				jxcCzrzService.add(jxcCzrz2);
			}
		}
		jxcPiciService.removeByOnlyid(id);
//		ddygPiciService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批次信息-批量删除")
	@ApiOperation(value="批次信息-批量删除", notes="批次信息-批量删除")
//	@RequiresPermissions("ddyht:jxc_pici:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		for (int i = 0; i < list.size(); i++) {
			String id = list.get(i);
			DdygPici byId = ddygPiciService.getById(id);
			String orstate = byId.getOrstate();
			String companyId = byId.getCompanyId();
			String model = byId.getModel();
			String number = byId.getNumber();
			String unitPrice = byId.getUnitPrice();
			String onlyid = byId.getOnlyid();
			String traner = byId.getTraner().trim();
			//去除空格

			log.info("批次删除数据"+byId.toString());
			String allamount = "";
			JxcPici jxcPici = jxcPiciService.getByOnlyid(onlyid);
			BigDecimal pcAmount = new BigDecimal(jxcPici.getAmount());

			JxcCzrz jxcCzrz = new JxcCzrz();
			jxcCzrz.setId(setczrzid(id));
			jxcCzrz.setCompanyId(companyId);
			jxcCzrz.setInfor("批次删除：金额"+String.valueOf(pcAmount));

			if (orstate.equals("1")){
				jxcCzrz.setType("进货单批次删除");
				jxcCzrz.setOther("pc端");
				jxcCzrzService.add(jxcCzrz);
				//进货
				JxcStock jxcstock = jxcStockService.getallamount(id, companyId);
				JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);

				allamount = jxcstock.getAmount();
				BigDecimal bigDecimal = new BigDecimal(allamount);
				BigDecimal subtract = bigDecimal.subtract(pcAmount);
				String s = String.valueOf(subtract);
				if (s.equals("0.00")){
					jxcStockService.removeById(id);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
					jxcCzrz1.setType("进货单删除");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}else {
					jxcstock.setAmount(s);
					jxcStockService.updateById(jxcstock);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
					jxcCzrz1.setType("进货单修改");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}
				if (jxcSupplier != null){
					BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
					jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(pcAmount)));
					jxcSupplierService.updateById(jxcSupplier);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(oldSupplieramount)+"->"+String.valueOf(oldSupplieramount.subtract(pcAmount)));
					jxcCzrz1.setType("供应商修改");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}
				if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
					ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(number))));
					BigDecimal multiply = new BigDecimal(unitPrice).multiply(new BigDecimal(number));
//					ddygInventory.setAmount(String.valueOf(new BigDecimal(ddygInventory.getAmount()).subtract(multiply)));
					ddygInventoryService.updateById(ddygInventory);
				}
			}else {
				jxcCzrz.setType("销货单批次删除");
				jxcCzrz.setOther("pc端");
				jxcCzrzService.add(jxcCzrz);

				JxcSale jxcSale = jxcSaleService.getallamount(id, companyId);
				JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);

				allamount = jxcSale.getAmount();
				BigDecimal bigDecimal = new BigDecimal(allamount);
				BigDecimal subtract = bigDecimal.subtract(pcAmount);
				String s = String.valueOf(subtract);
				if (s.equals("0.00")){
					jxcSaleService.removeById(id);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
					jxcCzrz1.setType("销货单删除");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}else {
					jxcSale.setAmount(s);
					jxcSaleService.updateById(jxcSale);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
					jxcCzrz1.setType("销货单修改");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}
				if (jxcCustom != null){
					BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
					jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(pcAmount)));
					jxcCustomService.updateById(jxcCustom);

					JxcCzrz jxcCzrz1 = new JxcCzrz();
					jxcCzrz1.setId(setczrzid(id));
					jxcCzrz1.setCompanyId(companyId);
					jxcCzrz1.setInfor("金额:"+String.valueOf(oldCustomamount)+"->"+String.valueOf(oldCustomamount.subtract(pcAmount)));
					jxcCzrz1.setType("客户修改");
					jxcCzrz1.setOther("pc端");
					R add = jxcCzrzService.add(jxcCzrz1);
				}
				if (ddygInventoryService.getBymodelcompany(model, companyId) != null){
					DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, companyId);
					ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).add(new BigDecimal(number))));
					ddygInventoryService.updateById(ddygInventory);
				}
			}
			jxcPiciService.removeByOnlyid(onlyid);
		}
//		this.ddygPiciService.removeByIds(list);
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "批次信息-通过id查询")
	@ApiOperation(value="批次信息-通过id查询", notes="批次信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DdygPici> queryById(@RequestParam(name="id",required=true) String id) {
		DdygPici ddygPici = ddygPiciService.getById(id);
		if(ddygPici ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ddygPici);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ddygPici
    */
//    @RequiresPermissions("ddyht:jxc_pici:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DdygPici ddygPici) {
        return super.exportXls(request, ddygPici, DdygPici.class, "批次信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("ddyht:jxc_pici:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DdygPici.class);
    }

	 public static String setczrzid(String id){
		 log.info("setczrzid:"+id);
		 String newid = id + UUID.randomUUID().toString().replace("-", "").substring(5, 9);
		 log.info("newid:"+newid);
		 return newid;
//		 String s = RandomUtil.generateRandomString1(id, id.length());
//		 return ddygPiciService.setczrzid(id);
//		 return id+s.substring(0,5);
//		 return System.currentTimeMillis() + "_" + id + "_" + UUID.randomUUID().toString().replace("-", "");
	 }
	 public void updateCzrzById(String id,String amount,String companyId){
		 JxcCzrz jxcCzrz = jxcCzrzService.getByIdAndOther(id, "应收应付",companyId);
		 jxcCzrz.setInfor(amount);
		 jxcCzrzService.updateById(jxcCzrz);
	 }
}
